package com.mani.geotag;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class CircularApplication extends Application{
	
	String mTwitterUserName;
	String mFacebookUserName;
	String mTwitterAccessKey;
	String mTwitterAccessSecret;
	String mFacebookAccessToken;
	boolean isLoaded = false;
	
	public void onCreate()
	{
        super.onCreate();
    	mTwitterAccessKey = null;
		mTwitterAccessSecret= null;
		mFacebookAccessToken = null;
        
	}

	public void onTerminate()
	{
		super.onTerminate();
	}
	
	public void updateTwitterToken(String key,String secret)
	{
		System.out.println("Update twitter token ***** "+key+":::"+secret);
		mTwitterAccessKey = key;
		mTwitterAccessSecret = secret;
		saveToDataBase();
	}

	public void setLoaded(boolean load) {
		isLoaded = load;
	}
	
	public String getTwitterAccessKey()
	{
		return mTwitterAccessKey;
	}

	public String getTwitterAccessSecret()
	{
		return mTwitterAccessSecret;
	}
	public void updateFacebookToken(String token)
	{
		mFacebookAccessToken = token;
		saveToDataBase();
	}

	public String getFacebookToken()
	{
		return mFacebookAccessToken;
	}

	public void updateTwitterUserName(String username)
	{
		mTwitterUserName = username;
	}

	public String getTwitterUserName()
	{
		return mTwitterUserName;
	}

	
	public void updateFacebookUserName(String username)
	{
		mFacebookUserName = username;
	}

	public String getFacebookUserName()
	{
		return mFacebookUserName;
	}

	public boolean isTwitterAutheticated()
	{
		if(mTwitterAccessKey == null && mTwitterAccessSecret == null)
			return false;
		else
			return true;
		
	}
	
	public boolean isFacebookAuthenticated()
	{
		if(mFacebookAccessToken == null)
			return false;
		else
			return true;

	}

	public void loadFromDataBase()
	{
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        Editor editor = null;
        mTwitterUserName = prefs.getString("twitterusername", null);
        mFacebookUserName = prefs.getString("facebookusername", null);
        mTwitterAccessKey = prefs.getString("twitteraccesskey", null);
        mTwitterAccessSecret = prefs.getString("twitteraccesssecret", null);
        mFacebookAccessToken = prefs.getString("facebooktoken", null);

	}
	
	public void saveToDataBase()
	{
	 	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        Editor editor = null;;
        editor = prefs.edit();
        
        editor.putString("twitterusername", mTwitterUserName);
        editor.putString("twitteraccesskey", mTwitterAccessKey);
        editor.putString("twitteraccesssecret", mTwitterAccessSecret);
        editor.putString("facebookusername", mFacebookUserName);
        editor.putString("facebooktoken", mFacebookAccessToken);
        
        editor.commit();
        isLoaded = false;
	}
}
